<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>单例设计模式 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter03.html" />
    
    
    <link rel="prev" href="./chapter01.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="2"
        data-chapter-title="单例设计模式"
        data-filepath="chapter02.md"
        data-basepath="."
        data-revision="Sun Feb 23 2020 13:59:40 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        类方法和实例方法
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        单例设计模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        继承的基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        继承的注意使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        对象相关的运算符和内置函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        多态的使用
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="new&#x548C;init&#x65B9;&#x6CD5;">__new__&#x548C;__init__&#x65B9;&#x6CD5;</h2>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&quot;&#x8FD9;&#x662F; init &#x65B9;&#x6CD5;&quot;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__new__</span><span class="hljs-params">(cls)</span>:</span>
        print(<span class="hljs-string">&quot;&#x8FD9;&#x662F; new &#x65B9;&#x6CD5;&quot;</span>)
        <span class="hljs-keyword">return</span> object.__new__(cls)

A()
</code></pre>
<h3 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h3>
<ul>
<li><code>__new__</code>&#x81F3;&#x5C11;&#x8981;&#x6709;&#x4E00;&#x4E2A;&#x53C2;&#x6570;cls&#xFF0C;&#x4EE3;&#x8868;&#x8981;&#x5B9E;&#x4F8B;&#x5316;&#x7684;&#x7C7B;&#xFF0C;&#x6B64;&#x53C2;&#x6570;&#x5728;&#x5B9E;&#x4F8B;&#x5316;&#x65F6;&#x7531;Python&#x89E3;&#x91CA;&#x5668;&#x81EA;&#x52A8;&#x63D0;&#x4F9B;</li>
<li><code>__new__</code>&#x5FC5;&#x987B;&#x8981;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x8FD4;&#x56DE;&#x5B9E;&#x4F8B;&#x5316;&#x51FA;&#x6765;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x8FD9;&#x70B9;&#x5728;&#x81EA;&#x5DF1;&#x5B9E;&#x73B0;<code>__new__</code>&#x65F6;&#x8981;&#x7279;&#x522B;&#x6CE8;&#x610F;&#xFF0C;&#x53EF;&#x4EE5;return&#x7236;&#x7C7B;<code>__new__</code>&#x51FA;&#x6765;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x6216;&#x8005;&#x76F4;&#x63A5;&#x662F;object&#x7684;<code>__new__</code>&#x51FA;&#x6765;&#x7684;&#x5B9E;&#x4F8B;</li>
<li><code>__init__</code>&#x6709;&#x4E00;&#x4E2A;&#x53C2;&#x6570;self&#xFF0C;&#x5C31;&#x662F;&#x8FD9;&#x4E2A;<code>__new__</code>&#x8FD4;&#x56DE;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;<code>__init__</code>&#x5728;<code>__new__</code>&#x7684;&#x57FA;&#x7840;&#x4E0A;&#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x4E00;&#x4E9B;&#x5176;&#x5B83;&#x521D;&#x59CB;&#x5316;&#x7684;&#x52A8;&#x4F5C;&#xFF0C;<code>__init__</code>&#x4E0D;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x503C;</li>
</ul>
<h2 id="&#x5355;&#x4F8B;&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;">&#x5355;&#x4F8B;&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;</h2>
<p>&#x4E3E;&#x4E2A;&#x5E38;&#x89C1;&#x7684;&#x5355;&#x4F8B;&#x6A21;&#x5F0F;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x4EEC;&#x65E5;&#x5E38;&#x4F7F;&#x7528;&#x7684;&#x7535;&#x8111;&#x4E0A;&#x90FD;&#x6709;&#x4E00;&#x4E2A;&#x56DE;&#x6536;&#x7AD9;&#xFF0C;&#x5728;&#x6574;&#x4E2A;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4E2D;&#xFF0C;&#x56DE;&#x6536;&#x7AD9;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF0C;&#x6574;&#x4E2A;&#x7CFB;&#x7EDF;&#x90FD;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x552F;&#x4E00;&#x7684;&#x5B9E;&#x4F8B;&#xFF0C;&#x800C;&#x4E14;&#x56DE;&#x6536;&#x7AD9;&#x81EA;&#x884C;&#x63D0;&#x4F9B;&#x81EA;&#x5DF1;&#x7684;&#x5B9E;&#x4F8B;&#x3002;&#x56E0;&#x6B64;&#x56DE;&#x6536;&#x7AD9;&#x662F;&#x5355;&#x4F8B;&#x6A21;&#x5F0F;&#x7684;&#x5E94;&#x7528;&#x3002;</p>
<p>&#x786E;&#x4FDD;&#x67D0;&#x4E00;&#x4E2A;&#x7C7B;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF0C;&#x800C;&#x4E14;&#x81EA;&#x884C;&#x5B9E;&#x4F8B;&#x5316;&#x5E76;&#x5411;&#x6574;&#x4E2A;&#x7CFB;&#x7EDF;&#x63D0;&#x4F9B;&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF0C;&#x8FD9;&#x4E2A;&#x7C7B;&#x79F0;&#x4E3A;&#x5355;&#x4F8B;&#x7C7B;&#xFF0C;&#x5355;&#x4F8B;&#x6A21;&#x5F0F;&#x662F;&#x4E00;&#x79CD;&#x5BF9;&#x8C61;&#x521B;&#x5EFA;&#x578B;&#x6A21;&#x5F0F;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x5B9E;&#x4F8B;&#x5316;&#x4E00;&#x4E2A;&#x5355;&#x4F8B;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Singleton</span><span class="hljs-params">(object)</span>:</span>
    __instance = <span class="hljs-keyword">None</span>
    __is_first = <span class="hljs-keyword">True</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__new__</span><span class="hljs-params">(cls, age, name)</span>:</span>
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> cls.__instance:
            cls.__instance = object.__new__(cls)
        <span class="hljs-keyword">return</span> cls.__instance

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, age, name)</span>:</span>
        <span class="hljs-keyword">if</span> self. __is_first: <span class="hljs-comment"># &#x4E0D;&#x4F1A;&#x518D;&#x521B;&#x5EFA;&#x7B2C;&#x4E8C;&#x4E2A;&#x5BF9;&#x8C61;</span>
            self.age = age
            self.name = name
            Singleton. __is_first = <span class="hljs-keyword">False</span>


a = Singleton(<span class="hljs-number">18</span>, <span class="hljs-string">&quot;&#x5F20;&#x4E09;&quot;</span>)
b = Singleton(<span class="hljs-number">28</span>, <span class="hljs-string">&quot;&#x5F20;&#x4E09;&quot;</span>)

print(id(a))
print(id(b))

print(a.age) <span class="hljs-comment"># 18</span>
print(b.age) <span class="hljs-comment"># 18</span>

a.age = <span class="hljs-number">19</span>
print(b.age)
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter01.html" class="navigation navigation-prev " aria-label="Previous page: 类方法和实例方法"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter03.html" class="navigation navigation-next " aria-label="Next page: 继承的基本使用"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
